package com.sh.d1.gameserver.cmdhandler;

import com.sh.d1.gameserver.Broadcaster;
import com.sh.d1.gameserver.model.User;
import com.sh.d1.gameserver.model.UserManager;
import com.sh.d1.gameserver.mq.MqProducer;
import com.sh.d1.gameserver.mq.VictorMsg;
import com.sh.d1.gameserver.msg.GameMsgProtocol;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 用户攻击指令处理器
 */
public class UserAttkCmdHandler implements ICmdHandler<GameMsgProtocol.UserAttkCmd> {
    /**
     * 日志对象
     */
    static private final Logger LOGGER = LoggerFactory.getLogger(UserAttkCmdHandler.class);

    @Override
    public void handle(MyCmdHandlerContext ctx, GameMsgProtocol.UserAttkCmd cmd) {
        if (null == ctx ||
            null == cmd) {
            return;
        }

        // 先拿到攻击用户 Id
        int attkUserId = ctx.getFromUserId();

        if (attkUserId <= 0) {
            return;
        }

        // 获取被攻击用户 Id
        int targetUserId = cmd.getTargetUserId();
        // 不管有没有打中都要广播攻击
        broadcastAttk(attkUserId, targetUserId);

        // 获取目标用户
        User targetUser = UserManager.getByUserId(targetUserId);
        if (null == targetUser) {
            LOGGER.error("目标用户为空, targetUserId = {}", targetUserId);
            return;
        }

        int subtractHp = 10;
        targetUser.currHp = Math.max(0, targetUser.currHp - subtractHp);

        LOGGER.info(
            "发生攻击行为, attkUserId = {}, targetUserId = {}",
            attkUserId,
            targetUserId
        );

        // 广播减血
        broadcastSubtractHp(targetUserId, subtractHp);

        if (targetUser.currHp <= 0) {
            // 广播死亡
            broadcastDie(targetUserId);

            if (!targetUser.died) {
                // 设置已经死亡
                targetUser.died = true;

                LOGGER.info("用户阵亡, userId = {}", targetUserId);

                VictorMsg mqMsg = new VictorMsg();
                mqMsg.winnerId = attkUserId;
                mqMsg.loserId = targetUserId;
                MqProducer.sendMsg("herostory_victor", mqMsg);
            }
        }
    }

    /**
     * 广播攻击
     *
     * @param attkUserId   攻击用户 Id
     * @param targetUserId 目标用户 Id
     */
    static private void broadcastAttk(int attkUserId, int targetUserId) {
        if (attkUserId <= 0) {
            return;
        }

        GameMsgProtocol.UserAttkResult.Builder resultBuilder = GameMsgProtocol.UserAttkResult.newBuilder();
        resultBuilder.setAttkUserId(attkUserId);
        resultBuilder.setTargetUserId(targetUserId);

        GameMsgProtocol.UserAttkResult newResult = resultBuilder.build();
        Broadcaster.broadcast(newResult);
    }

    /**
     * 广播减血
     *
     * @param targetUserId 目标用户 Id
     * @param subtractHp   减血量
     */
    static private void broadcastSubtractHp(int targetUserId, int subtractHp) {
        if (targetUserId <= 0 ||
            0 == subtractHp) {
            return;
        }

        GameMsgProtocol.UserSubtractHpResult.Builder resultBuilder = GameMsgProtocol.UserSubtractHpResult.newBuilder();
        resultBuilder.setTargetUserId(targetUserId);
        resultBuilder.setSubtractHp(subtractHp);

        GameMsgProtocol.UserSubtractHpResult newResult = resultBuilder.build();
        Broadcaster.broadcast(newResult);
    }

    /**
     * 广播死亡
     *
     * @param targetUserId 目标用户 Id
     */
    static private void broadcastDie(int targetUserId) {
        if (targetUserId <= 0) {
            return;
        }

        GameMsgProtocol.UserDieResult.Builder resultBuilder = GameMsgProtocol.UserDieResult.newBuilder();
        resultBuilder.setTargetUserId(targetUserId);

        GameMsgProtocol.UserDieResult newResult = resultBuilder.build();
        Broadcaster.broadcast(newResult);
    }
}
